import datetime
from typing import List, Literal
from pydantic import Field, BaseModel
from package.fastapi.schema import Page, BatchId


class _Field(BaseModel):
    name: str = Field(..., title='字段名')
    type: Literal['keyword', 'text', 'ip', 'long', 'integer', 
                  'short', 'byte', 'float', 'double', 'date', 
                  'geo_point', 'boolean', 'object'] = Field(..., title='字段类型')
    alias: str = Field('', title='显示别名')
    required: bool = Field(False, title='是否必填')


class _Policy(BaseModel):
    cold_days: int = Field(7, title='转冷日期')
    del_days: int = Field(180, title='保留日期')


class EditModel(BaseModel):
    fields: List[_Field] = Field([], title='字段')
    policy: _Policy = Field({'cold_days': 7, 'del_days': 180}, title='策略')
    

class CreateModel(EditModel):
    name: str = Field(..., title='')


class ModelOut(CreateModel):
    id: str = Field(..., title='ID')
    created_at: datetime.datetime = Field(None, title='')

    class Config:
        orm_mode = True
        json_encoders = {datetime.datetime: lambda dt: int(dt.timestamp() * 1000)}


class PageModels(Page):
    """获取页码条目"""
    data: List[ModelOut] = Field(..., title='')
